package forms;

import java.util.TreeMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

import models.NewsModel;

public class NewsForm {
	public static TreeMap checkValid(HttpServletRequest request, NewsModel news) {
		TreeMap form = new TreeMap();
		TreeMap checkValid = new TreeMap();
		
		String url = (String) request.getParameter("url");
		String published = (String) request.getParameter("published");
		String hotNews = (String) request.getParameter("hotNews");
		String title = (String) request.getParameter("title");
		String description = (String) request.getParameter("description");
		String category = (String) request.getParameter("category");
		
		if (url == null || url.equals("")) {
			checkValid.put("url", "Url is not empty!");
			news.setUrl("");
		} else {
			news.setUrl(url);
		}
		
		if (title == null || title.equals("")) {
			checkValid.put("title", "Title is not empty!");
			news.setTitle("");
		} else {
			news.setTitle(title);
		}
		
		if (description == null || description.equals("")) {
			checkValid.put("description", "Description is not empty!");
			news.setDescription("");
		} else {
			news.setDescription(description);
		}
		
		if (published != null)
			news.setPublished(new Boolean(published));
		
		if (hotNews != null)
			news.setHotNews(new Boolean(hotNews));
		
		if (category != null && !category.equals(""))
			news.setCategory(category);
		
		form.put("checkValid", checkValid);
		form.put("news", news);
		
		return form;
	}
}
